<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="./html/common.css" />
    <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="../ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
	<div class="content">
	
	<div class = "setTitle">文本编辑</div>
		<div class="editor">
			<div class="left">
			<div class="title1">标题</div>
			<input class="titleInput" id = "title" type="text" placeholder="这里填写博文标题" value="{$article.title}"></input>
			<div class="title1">正文</div>
			 <div id="editor" type="text/plain" style="height:300px">
			 </div>
		      <p >博文封面： <input id="titleimg" type="file" name="file" /><input type="submit" onclick="submitImg()" value="提交"></input>
			
			<div class="thetitleimg">
				当前封面:<span class="titileimgname">{if $article.titleimg!=NULL}{$article.titleimg}{else}无{/if}</span>
				<img class="titileimgname" src="{if $article.titleimg!=NULL}../source/picture/{$article.titleimg}{/if}">
			</div>
			<script type="text/javascript">
				var ue = UE.getEditor('editor');
				ue.ready(function() {
					getArticle(ue);
				});
					function createXmlHttp() {
					  var xmlHttp = null;
					  try {
						xmlHttp = new XMLHttpRequest();
					  } catch (e) {
						try {
						  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
						} catch (e) {
						  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
						}
					  }
					  return xmlHttp;
					}
					  
					function submitForm() {
					  var xmlHttp = createXmlHttp();
					  if('{$article}'.length==0)
						var url = './trans.php?action=subarticle';
					  else
						var url = './trans.php?action=uparticle';
					  var postData = new FormData();
					  postData.append('content',ue.getContent());
					  postData.append('title',document.getElementById("title").value);
					  postData.append('artkind',document.getElementById("artkind").value);
					  postData.append('state',document.getElementById("state").value);
					  postData.append('author',document.getElementById("author").value);
					  if(document.getElementsByClassName('titileimgname')[0].innerHTML!='无'){
					  	postData.append('titleimg',document.getElementsByClassName('titileimgname')[0].innerHTML);
					  }
					  if(document.getElementById('top').checked){
						postData.append('istop',"1");
					  }else{
						postData.append('istop',"0");
					  }		
					  if(document.getElementById('banr').checked){
						postData.append('isbanr',"1");
					  }else{
						postData.append('isbanr',"0");
					  }
					  if(document.getElementById('show').checked){
						postData.append('isshow',"1");
					  }else{
						postData.append('isshow',"0");
					  }

					  if(document.getElementById('page').checked){
						postData.append('ispage',"1");
					  }else{
						postData.append('ispage',"0");
					  }

					  if('{$article}'.length!=0){
						postData.append('id',"{$article.id}");
					  }
					  if(document.getElementById('date').value)
						postData.append('time',Date.parse(document.getElementById('date').value)/1000);
					  xmlHttp.open("POST", url, true);
					  xmlHttp.onreadystatechange = function() {
						if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
						  if(xmlHttp.responseText == '1') {

							location.replace('article.php?') 
						  }
						 // alert(xmlHttp.responseText);
						}
					  }
					  xmlHttp.send(postData);
					}
					function submitImg(){
						var xmlHttp = createXmlHttp();
						var url = './trans.php?action=subarticleimg';
					 	var postData = "";
					  	var form = new FormData();
					  	form.append("file",document.getElementById("titleimg").files[0]);
					  	xmlHttp.open("POST", url, true);
					  	xmlHttp.onreadystatechange = function() {
						if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {					
						  if(xmlHttp.responseText) {
						  	document.getElementsByClassName('titileimgname')[0].innerHTML = xmlHttp.responseText;
						  	document.getElementsByClassName('titileimgname')[1].src="../source/picture/"+document.getElementsByClassName('titileimgname')[0].innerHTML
						  }
						}
					  }
						xmlHttp.send(form);				
					}
					function getArticle(editor){
						var xmlHttp = createXmlHttp();
						var url = './trans.php?action=getarticletext';
						var form = new FormData();
						form.append("id","{$article.id}")
						xmlHttp.open("POST", url, true);
						xmlHttp.onreadystatechange = function() {
						if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {					
						  if(xmlHttp.responseText){
						  	editor.setContent(xmlHttp.responseText);
						  }
						}
					  }
						xmlHttp.send(form);										
					}
			</script>
			</div>
			<div class="right">
				<button class="pubButton" type='submit' onclick="submitForm('1')">{if $article}更新{else}发布{/if}</button>
				<div class="config">
					分类
					<select id="artkind">
						<option value="0" selected>未分类</option>
						{foreach $artkindlist as $value}
							{if $value.id==$article.kindid}
							<option value="{$value.id}" selected>{$value.name}</option>
							{else}
							<option value="{$value.id}">{$value.name}</option>
							{/if}
						{/foreach}
					</select>
				</div>
				<div class="config">
					状态
					<select id="state">
						<option value="0" {if $article.state==0}selected{/if}>公开</option>
						<option value="1" {if $article.state==1}selected{/if}>私密</option>
						<option value="2" {if $article.state==2}selected{/if}>草稿</option>
					</select>
				</div>
				<div class="config">
					作者
					<input class="authorInput" id = "author" type="text" value="{if $article}{$article.author}{else}个快快{/if}"></input>
				</div>
				<div class="config">
					日期
					<input type="date" id="date" value="{if $time}{$time}{else}{/if}"></input>
				</div>
				<div class="config">
					<input type="checkbox" class="check" id="top" {if $article.istop==1}checked{/if}>置顶
				</div>
				<div class="config">
					<input type="checkbox" class="check" id="banr" {if $article.isban==1}checked{/if}>禁止评论
				</div>
				<div class="config">
					<input type="checkbox" class="check" id="show" {if $article.isshow==1}checked{/if}>首页展示
				</div>
				<div class="config">
					<input type="checkbox" class="check" id="page" {if $article.ispage==1}checked{/if}>专题模板
				</div>
			</div>
			
		</div>
	
	</div>
</body>
</html>